/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package htmlunit.code;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import hibernate.Dao.crawlerDao;
import java.text.SimpleDateFormat;
import java.util.Date;
import htmlunit.mode.*;

/**
 *
 * @author TiTa
 */
public class lay_hinh_chua_thamso {//extends Thread-------------------
    public lay_hinh_chua_thamso(){}
    
    
    Link lh=new Link();
    Hinh h=new Hinh();
    DomElement doc;
    int i2;
    public  lay_hinh_chua_thamso(Link i1){
    this.lh=i1;
    }
    
    //Lay gio he thong
Date gioD=new Date(System.currentTimeMillis()); 

//Khai bao dinh dang ngay thang
SimpleDateFormat đinhangngay= new SimpleDateFormat("dd MM yyyy"); 

//parse ngay thang sang dinh dang va chuyen thanh string.
String homnayS=đinhangngay.format(gioD.getTime());  


     public  void lay_hinh_khong_trang(String url) throws EnumConstantNotPresentException,ExceptionInInitializerError{
      String url_media=url;
           String[] arr;
      arr = url_media.split("/");
    
         try{
         java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
         java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);
     //--!end warning
    final WebClient webClient = new WebClient();
    // webClient.getOptions().setTimeout(2000);
    webClient.getOptions().setJavaScriptEnabled(false);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setUseInsecureSSL(true);
    webClient.waitForBackgroundJavaScript(10000);
   //  String[] arr;
   //   arr = url.split("/");
  //  url="https://www.facebook.com/pages/V%E1%BB%ABng-%C6%A1i-m%E1%BB%9F-ra-Cafe/141160225904005?id=141160225904005&sk=photos_stream";//ket noi trang
  
    
    
    if(arr[3].toString().equals("media")){
   
    
    }else{ kiem_tra_loi_404 k404=new kiem_tra_loi_404();//kiem tra boi vi co mot so trang cau truc "page nhung khong co photo"
    
        if(k404.exists(them_photos_stream(url))==false){
            System.out.print("trang này không có hình");//ko chay vao day
        }else{
            //System.out.print("trang may khong ton tai true");
            url=them_photos_stream(url);
        }
    
    }
    
 
    final HtmlPage page=webClient.getPage(url);
    //lay dia chi ok
  /*  final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@class='fbTimelineSummarySectionWrapper']").get(0);
       doc=div.getElementsByTagName("span").get(6);
     
    String link=doc.getElementsByTagName("a").get(0).getTextContent();
    System.out.println(link);
    //////////////////////////
    //lay dia chi tot hon
      final HtmlSpan div1 = (HtmlSpan) page.getByXPath("//span[@class='fbProfileBylineLabel']").get(1);
       
       doc=div1.getElementsByTagName("span").get(0);
       String link1=doc.getElementsByTagName("a").get(0).getTextContent();
       System.out.println(link1);

        //dien thoai
       final HtmlSpan div2 = (HtmlSpan) page.getByXPath("//span[@class='fbProfileBylineLabel']").get(2);
       
       String d=div2.getElementsByTagName("span").get(0).getTextContent();
    
       System.out.println(d);
*/
      //lay hinh
    int so_hinh=page.getByXPath("//div[@class='tagWrapper']").size();
    System.out.println(so_hinh);
    for(int i=0;i<so_hinh;i++){
    
       final HtmlDivision hinh = (HtmlDivision) page.getByXPath("//div[@class='tagWrapper']").get(i);
       
        String the_i=hinh.getElementsByTagName("i").get(0).getAttribute("style");
          String da_cat=null;
          
            da_cat=cat_theo_ki_tu_fb(the_i);
            System.out.println(the_i);  //dia chi chua cat
            
            
         if(chuoi_la_fb_sc(da_cat).equals("sc")){
         
         da_cat= cat_theo_ki_tu_sc(the_i);
         }else if(chuoi_la_fb_sc(da_cat).equals("fb"))
         {
            da_cat=cat_theo_ki_tu_fb(the_i);
         
         }
    
        
        
         System.out.println(da_cat+"\n");//dia chi da cat rui
         
         
          tai_hinh_htmlunit thh=new tai_hinh_htmlunit(homnayS+arr[3].toString()+" "+"hinh"+" "+(i+1));
          
             thh.chuoi_hinh(da_cat);
             
             h.setLinkHinh(da_cat);
             h.setLink(lh);
            
             h.setTenHinh(thh.ten_hinh());
             h.setDiaChiTrenMay(thh.duong_dan_hinh());
             crawlerDao cd=new crawlerDao();
             cd.capnhat(h);
             
           
             }   //end for lay link hinh
   
         }
         
         catch(Exception ex){
         
         ex.printStackTrace();
         }
         
     }
     private String chuoi_la_fb_sc(String s){
     //  int do_dai=s.length();
       //   String input_cat_dau=s.substring(22, do_dai-2);
       String ss=s;
             ss = s.substring(8,10);
            // System.out.println(ss);
           return ss;
     }
     public String cat_theo_ki_tu_sc(String input){
        int do_dai=input.length();
         String input_cat_dau=input.substring(22, do_dai-2);
       
          String ss="";
        String[] arr;
          
      arr = input_cat_dau.split("/");
   if(arr[5].equals("p417x417")){
              arr[5]="p640x640";
           
      for (int i = 0; i < arr.length; i++) {
        ss=ss+arr[i]+"/";
         
      }
      ss=ss.substring(0,input_cat_dau.length());

      System.out.println(ss);
   }else{return input_cat_dau;}
     return ss;
     }
      public String cat_theo_ki_tu_fb(String input)
    {
        String arr[];
         arr = input.split("/");
          String input_cat_dau=null;
         if(arr[5].equals("p417x417")){
          
     
     String ss=null;
         
        int do_dai=input.length();
        if(do_dai>150){  input_cat_dau=input.substring(22, input.length()-2);}else{
         input_cat_dau=input.substring(22, do_dai-2);
   //khong can thiet
     //  String[] arr;
      // arr= input.split("");
      //
         ss=input_cat_dau;
         int do_dai1=input_cat_dau.length();
       input_cat_dau = input_cat_dau.substring(0,58);
          //System.out.println(input_cat_dau);
            ss = ss.substring(67,do_dai1);
           //  System.out.println(ss);
             input_cat_dau=input_cat_dau+ss;
           //   System.out.println(input_cat_dau);
        }
         }else{   int l=input.length();
    input_cat_dau= input.substring(22,l-2);    
     //System.out.println(ss);
         }
       
        return input_cat_dau;
    }
   

private String them_photos_stream(String url){
      
        String s=url+"/photos_stream";
        return s;
  }
     
     public static void main(String[] args){
     
         
         lay_hinh_chua_thamso lh=new lay_hinh_chua_thamso();
        String url="https://www.facebook.com/media/set/?set=a.10151253390061951.452237.135833866950&type=1";
            lh.lay_hinh_khong_trang(url);
          //   lh.cat_theo_ki_tu();
             // lh.DownloadPage("https://www.facebook.com/hardrockcafevietnam");
            
         }
     
}
